<!-- 广告图 启动页-->
<template>
	<view class="content page">
		<view v-if="startUpData.startUp == 1">
			<view class="advertising">
				<image :src="imgUrl + startUpData.start_img" ></image>
			</view>
			<view class="fiexd_bottom">
				<image :src="imgUrl+startUpData.logo_img" mode="aspectFit"></image>
				<view v-show="isShow" class="indexBtn" @click="vaulting">跳过<text>{{cudTime}}</text>S</view>
			</view>
		</view>
	</view>
</template>

<script>
	import API from '../../api/index.js' 
	let that
	export default {
		data() {
			return {
				shareData: {
					title: '分享的标题哦',
					desc: '这是默认的转发文案，用户可以直接发送，也可以在发布器内修改',
					imageUrl: 'http://pic1.zhimg.com/50/v2-deff985cfcb7690bc71fdf8676438146_hd.jpg',
				},
			
			     bid: 'lNKK_1nc2N1rCDmiRxeA2g==',  

				imgUrl: this.imgUrl,
				cudTime: 5, // 倒计时默认值
				isShow: false, // 是否开起倒计时
				dtime: '', // timer
				startUpData: '',
				/**
				 * pageType:分享页面的类型 
				 *  0首页(包括活动首页) 1商品页(普通/特价商品)  
				 *  2商品页(团购商品)  3分享团购  4文章详情 
				 *  5文章列表  6某个视频
				 *  99其他页面分享
				 * **/
				pageType: '', // 分享页面的类型  
				share_v_id: '', // 分享vid
				options: ''
			}
		},
		onShow() {
			let that = this;

			let s_id = uni.getStorageSync('share_v_id')
			   uni.clearStorageSync(); // 避免出现串号情况
			   uni.setStorageSync('share_v_id', s_id);
			   uni.setStorageSync('bid', that.bid);
			   uni.setStorageSync('buyState', 1);
			  
			   // #ifndef APP-PLUS
			   that.startUp();
			   // #endif
		},
		onLoad(e) {
		  let that = this;
	
			that.options = e;
			   that.pageType = e.pageType ? e.pageType : e.p;
			   that.share_v_id = e.share_v_id ? e.share_v_id : e.sid;
			   
			   if(e.bid && getApp().globalData.isDemo == 1){
			    
			    uni.setStorageSync('bid', e.bid)
			   }
			   if(e.scene){ // 太阳码来的
			    let queryStr = decodeURIComponent(e.scene).split('&');
			    var obj = {};
			    if(queryStr[0] == 'p=3'){// 分享团购邀请海报 单独处理 避免参数过长
			     obj.pageType = 3
			     let arr = queryStr[1].split(',')
			     obj.gid = arr[0]
			     obj.aid = arr[1]?arr[1]:''
			     obj.group_id = arr[2]?arr[2]:''
			     obj.sid = arr[3]?arr[3]:''
			    }else{
			     for(let i in queryStr){
			      var query = queryStr[i].toString().split('=');
			      if(query[0]&&query[1]){
			       obj[query[0]] = query[1]
			      }
			     }
			    }
			    that.options = obj
			    if(obj.p)
			     that.pageType = obj.p
			    if(obj.pageType)
			     that.pageType = obj.pageType
			    if(obj.sid)
			     that.share_v_id = obj.sid
			   }
			   uni.setStorageSync('share_v_id', that.share_v_id);
		},
		
		methods: {
			getClientId() {//获取客户端ID和版本号
				var clientid = '';
				plus.device.getInfo({// 苹果系统
					success: function(e) {
						if (e.uuid) {
							clientid = e.uuid;
							uni.setStorageSync('openid', clientid.split(',')[0]);
						}
					},
					fail: function(e) {}
				});
				plus.device.getAAID({// 安卓系统
					success: function(e) {
						if (e.aaid) {
							clientid = e.aaid;
							uni.setStorageSync('openid', clientid.split(',')[0]);
						}
					},
					fail: function(e) {}
				});
				if (clientid == '') {//老版本、安卓模拟器
					clientid = plus.device.uuid;
					uni.setStorageSync('openid', clientid.split(',')[0]);
				}
				that.startUp();
			},
	
			startUp() {
				let that = this;
				let url = '/zzj_singleSaleApi/startUp';
				if(getApp().globalData.isDemo == 1){ // 演示版本
					url = '/singleSaleDemo/startUp'
				}
				
				that.$http.post({
					url: url,
					data: {
						bid: that.bid
					}
				}).then(res => {
					
					if (res.errcode == '100') {
						if (res.bid) {
							uni.setStorageSync('bid', res.bid)
							API.user.getOpenid().then(openid=>{
								if(openid){
									API.user.getVid()
								}
							})
						}
						
						if (res.appid){
							uni.setStorageSync('appid', res.appid);
							uni.setStorageSync('secret', res.secret);
						}	
						if (res.themeConfig) {
							uni.setStorageSync('themeConfig', res.themeConfig)
							if(res.themeConfig.colorVal1){
								getApp().globalData.color = res.themeConfig.colorVal1
								uni.setNavigationBarColor({
									backgroundColor: res.themeConfig.colorVal1,
									animation: {
									  duration: 10
									}
								})
							}
								
							if(res.themeConfig.colorVal2)
								getApp().globalData.color_aid = res.themeConfig.colorVal2
							if(res.themeConfig.tabBarIds && res.themeConfig.tabBarIds.indexOf(",8,") > 0){ // 有quan zi
								res.themeConfig.tabBar.forEach((item)=>{
									if(item.id == 8){
										getApp().globalData.postConfig.postTitle = item.title
										getApp().globalData.postConfig.showPost = true
									}
								})
							}
							if(res.themeConfig.tabBarIds && res.themeConfig.tabBarIds.indexOf(",7,") > 0){ // 有购物车
								res.themeConfig.tabBar.forEach((item)=>{
									if(item.id == 7){
										getApp().globalData.cartName = item.title
									}
								})
							}
						}
						if (res.v_id) 
							uni.setStorageSync('vid', res.v_id)
						if (res.open_id)
							uni.setStorageSync('openid', res.open_id)
						if (res.data.share)
							uni.setStorageSync('shareData', res.data.share)
						if (res.userinfo)
							uni.setStorageSync('userinfo', res.userinfo)
						uni.setStorageSync('mode', res.data.mode ? res.data.mode : '0');
						uni.setStorageSync('startUp', res.data);
						if(res.data.enableLbs.state == 1){
							let city = res.data.enableLbs.city?res.data.enableLbs.city:'重庆'
							uni.setStorageSync('city', city);
						}
						
						that.startUpData = res.data;
						if(res.data.way_power){
							that.copy()
						}
						if (uni.getStorageSync('path') == '13') { // 避免店铺列表页跳转回首页重新出现启动页
							that.startUpData.startUp = 0
						}
						if (that.startUpData.startUp != 1) { //  0关闭启动页  1开启启动页
							if (that.startUpData.act_id == 0) { // 走home页
						       that.toPage()
							}
							that.toPage()
						} else {
							that.cudTime = that.startUpData.countdown ? that.startUpData.countdown : that.cudTime;
							that.isShow = true;
							that.dtime = setInterval(() => {
								if (that.cudTime > 0) {
									that.cudTime -= 1
									
								} else {
									clearInterval(that.dtime);
									that.toPage()
								}
							}, 1000)
						}
					}
				})
			},
			
			
			
			
			copy(){
				uni.setClipboardData({
					data: that.startUpData.way_power,
					success(res) {
						uni.hideToast();
						console.log('复制成功！')
					},
					fail(res) {
						console.log('复制失败：'+JSON.stringify(res))
					}
				});
			},
			toPage: function() {
				let that = this;
				if (that.pageType && that.pageType != 99) { // 分享来的
					if (that.pageType == 0) { // 分享链接来的首页
						let tab = 1
						if(that.options.tab){
							tab = that.options.tab
						}
						uni.setStorageSync('tab',tab)
						uni.reLaunch({
							url: "/pages/tabbar/tabbar"
						})
					}
					let url
					if (that.pageType == 3) { // 分享链接来的 参与团购
						url = '/pages/group/tuxedo?gid=' + that.options.gid + '&act_id=' + that.options.aid + '&group_id=' +
							that.options.group_id + '&share_v_id=' + that.share_v_id+'&fromShare=1'
					}
					if (that.pageType == 4) { // 分享链接来的 某篇文章
						url = '/otherPage/article/artDetail?artId=' + that.options.artId+'&fromShare=1'
					}
					if (that.pageType == 5) { //文章列表
						url = '/pages/article/artList?listId=' + that.options.listId+'&fromShare=1'
					}
					if (that.pageType == 6) { //某个视频
						url = '/pages/video/video?goodsid=' + that.options.gid + '&classifyId=' + that.options.cid+'&fromShare=1'
					}
					if (that.pageType == 7) { // 单商品特价活动
						url = '/activityPage/bargain_goods/a_single_commodity?act_id=' + that.options.act_id+'&fromShare=1'
					}
					if (that.pageType == 8) { // 单商品团购
						url = '/activityPage/group/singleitemliner?act_id=' + that.options.act_id+'&fromShare=1'
					}
					if (that.pageType == 9) { // 多商品首页
						url = '/activityPage/bargain_goods/more_goods_page?act_id=' + that.options.act_id+'&fromShare=1'
					}
					if (that.pageType == 10) { // 分享链接来的 商品详情页（合并版）
						url = '/pages/goods/goodsDetail?goodsid=' + that.options.gid+'&fromShare=1'
					}
					if (that.pageType == 11) { // ft
						url = '/otherPage/quanZ/post?fromShare=1'
					}
					if (that.pageType == 12) { // 我的qz
						url = '/otherPage/quanZ/myPosts?fromShare=1'
					}
					if (that.pageType == 13) { // qz广场1
						url = '/pages/post/postIndex?fromShare=1'
					}
					if (that.pageType == 14) { // qz广场2
						url = '/pages/post/postIndex2?fromShare=1'
						if(getApp().globalData.postConfig.isTabbar){
							uni.switchTab({
								url: url
							})
							return
						}
					}
					if (that.pageType == 15) { // 门店首页
						url = '/pages/store/storeIndex?shop_id='+that.options.shop_id+'&fromShare=1'
					}
					if (that.pageType == 16) { // 跳表单（gid代替，不然后台分享需要新增参数）
						url = '/pages/reserve/formSubmit?ordId='+that.options.gid+'&fromShare=1'
					}
					if (that.pageType == 17) { // 跳游戏（cid代替cid，gid代替type,不然后台分享需要新增参数）
						if (that.options.gid == 'default') {//摇一摇
							url = '/otherPage/lottery/shark?cid='+that.options.cid+'&fromShare=1'
						} else if (that.options.gid == 'marquee') {//跑马灯
							url = '/otherPage/lottery/good_luck?cid='+that.options.cid+'&fromShare=1'
						} else if (that.options.gid == 'slotMachine') {//老虎机
							url = '/otherPage/lottery/tiger?cid='+that.options.cid+'&fromShare=1'
						} else if (that.options.gid == 'bigWheel') {//大转盘
							url = '/otherPage/lottery/roulette?cid='+that.options.cid+'&fromShare=1'
						}
					}
					if (that.pageType == 18) { // 芬霄中心
						url = '/pages/distributors/fenxiaoCenter?fromShare=1'
					}
					if (that.pageType == 19) { // 免费领取课程
						url = '/orderPage/coursePay/freeCourse?id='+that.options.id
					}
					uni.redirectTo({
						url: url
					})
				} else { //其他页面来的直接走正常进入流程
					if(getApp().globalData.NativeTabbar){
						uni.switchTab({
							url: "/pages/home/home"
						})
					}else{
						let tab = 1
						if(that.options.tab){
							tab = that.options.tab
						}
						uni.setStorageSync('tab',tab)
						uni.reLaunch({
							url: "/pages/tabbar/tabbar"
						})
					}
				}
			},
			vaulting: function() {
				let that = this;
				that.toPage()
				clearInterval(that.dtime);
			}
		},
		onShareAppMessage() {
			let shareData = uni.getStorageSync('shareData');
			let vid = uni.getStorageSync('vid');
			return {
				title: shareData.title,
				desc: shareData.desc,
				path: '/pages/index/index?share_v_id=' + vid,
				imageUrl: shareData.imageUrl
			}
		},
	}
</script>

<style scoped lang="scss">
	.page{
		overflow: hidden;
		display: flex;
		background: #FFFFFF;
		height: 100vh;
		.advertising {
			width: 100%;
			height: 100%;
			position: absolute;
			image {
				width: 100%;
				height: 100%;
			}
		}
		.fiexd_bottom {
			width: 100%;
			height: 300rpx;
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			background: #FFFFFF;
			z-index: 5;
			image {
				width: 200rpx;
				height: 200rpx;
				position: absolute;
				left: 50%;
				margin-left: -100rpx;
				top: 50%;
				margin-top: -100rpx;
			}
			.indexBtn {
				padding: 0 20rpx;
				line-height: 50rpx;
				border-radius: 64rpx;
				border: 1rpx solid #999999;
				color: #333333;
				position: absolute;
				right: 30rpx;
				top: 40%;
				text {
					color: #FF0000;
					font-weight: bold;
					font-size: 32rpx;
					padding: 0 4rpx;
				}
			}
		}
		
	}
</style>
